package com.dqhl.smartstable.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.dqhl.smartstable.R;
import com.dqhl.smartstable.adapter.AlarmLogListAdapter;
import com.dqhl.smartstable.base.BaseActivity;
import com.dqhl.smartstable.model.AlarmLog;
import com.dqhl.smartstable.model.PersonalInfo;
import com.dqhl.smartstable.util.Config;
import com.dqhl.smartstable.util.Constant;
import com.dqhl.smartstable.util.Dlog;
import com.dqhl.smartstable.util.JsonUtils;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

/**
 * Created by mac on 16/10/18.
 * 个人信息
 */
public class PersonalInfoActivity extends BaseActivity implements View.OnClickListener {

    private ImageView iv_top_back;
    private TextView tv_top_center;
    private TextView tv_top_right;
    private EditText et_name;
    private EditText et_sex;
    private TextView tv_birthday;
    private EditText et_region;
    private EditText et_phone;

    private boolean isEditable = false;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_personal_info);

        initView();
        initData();
    }

    private void initData() {
        RequestParams params = new RequestParams(Config.Api.user_info);
        params.addBodyParameter("token", sp.getString(Constant.TOKEN, ""));
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Dlog.e(result);
                String msg = JsonUtils.getMsg(result);
                String code = JsonUtils.getCode(result);
                String data = JsonUtils.getData(result);
                if ("2000".equals(msg)) {
                    PersonalInfo personalInfo = JSON.parseObject(data, PersonalInfo.class);
                    et_name.setText(personalInfo.getUsername());
                    if ("1".equals(personalInfo.getSex())) {
                        et_sex.setText("男");
                    } else if ("2".equals(personalInfo.getSex())) {
                        et_sex.setText("女");
                    }
                    tv_birthday.setText(personalInfo.getBirthday());
                    et_region.setText(personalInfo.getRegion());
                    et_phone.setText(personalInfo.getPhone());
                } else {
                    toast(code);
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

    private void initView() {
        iv_top_back = (ImageView) findViewById(R.id.iv_top_back);
        tv_top_center = (TextView) findViewById(R.id.tv_top_center);
        tv_top_center.setText("个人信息");
        tv_top_right = (TextView) findViewById(R.id.tv_top_right);
        if (isEditable) {
            tv_top_right.setText("保存");
        } else {
            tv_top_right.setText("编辑");
        }

        et_name = (EditText) findViewById(R.id.et_name);
        et_sex = (EditText) findViewById(R.id.et_sex);
        tv_birthday = (TextView) findViewById(R.id.tv_birthday);
        et_region = (EditText) findViewById(R.id.et_region);
        et_phone = (EditText) findViewById(R.id.et_phone);

        iv_top_back.setOnClickListener(this);
        tv_top_right.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_top_back:
                finish();
                break;
            case R.id.tv_top_right: //编辑
                if (isEditable) {
                    editInfo();
                } else {
                    tv_top_right.setText("保存");
                    et_phone.setFocusable(true);
                    et_phone.setFocusableInTouchMode(true);
                    et_phone.requestFocus();
                    et_region.setFocusable(true);
                    et_region.setFocusableInTouchMode(true);
                    et_region.requestFocus();
                    et_sex.setFocusable(true);
                    et_sex.setFocusableInTouchMode(true);
                    et_sex.requestFocus();
                    et_name.setFocusable(true);
                    et_name.setFocusableInTouchMode(true);
                    et_name.requestFocus();
                    isEditable = true;
                }
                break;
        }
    }

    /**
     * 编辑
     */
    private void editInfo() {
        String strUsername = et_name.getText().toString().trim();
        String sex = et_sex.getText().toString().trim();
        String strSex = "";
        if ("男".equals(sex)) {
            strSex = "1";
        } else if ("女".equals(sex)) {
            strSex = "2";
        }
        String strRegion = et_region.getText().toString().trim();
        String strPhone = et_phone.getText().toString().trim();
        Dlog.e("传递的参数:" + strUsername + ";" + strSex + ";" + strRegion + ";" + strPhone);
        RequestParams params = new RequestParams(Config.Api.edit_info);
        params.addBodyParameter("token", sp.getString(Constant.TOKEN, ""));
        params.addBodyParameter("username", strUsername);
        params.addBodyParameter("sex", strSex);
        params.addBodyParameter("birthday", "1999-01-01");
        params.addBodyParameter("region", strRegion);
        params.addBodyParameter("phone", strPhone);
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Dlog.e(result);
                String msg = JsonUtils.getMsg(result);
                String code = JsonUtils.getCode(result);
                String data = JsonUtils.getData(result);
                if ("2000".equals(msg)) {
                    tv_top_right.setText("编辑");
                    isEditable = false;
                    et_phone.setFocusable(false);
                    et_region.setFocusable(false);
                    et_sex.setFocusable(false);
                    et_name.setFocusable(false);

                }
                toast(code);
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }
}
